{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from textrank4zh import TextRank4Keyword, TextRank4Sentence\n",
    "from collections import defaultdict\n",
    "import networkx as nx\n",
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "TextRank4Sentence??"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def pairfilter(wp):\n",
    "    pos = (\"ns\", \"n\", \"vn\", \"v\",'eng')\n",
    "    return (wp.flag in pos and len(wp.word.strip()) >1)\n",
    "\n",
    "def get_connection_graph(tokens, window=4):\n",
    "    connections = defaultdict(list)\n",
    "    tokens_filter = [token for token in tokens if pairfilter(token)]\n",
    "    for i, wp in enumerate(tokens_filter):\n",
    "        ##遍历窗口，把符合条件的词加入边的关系\n",
    "        for j in range(i+1, i+window):\n",
    "            if j >= tokens_len:break\n",
    "            #窗口里的词不符合删选条件\n",
    "            if not pairfilter(tokens[j]):\n",
    "                continue \n",
    "            connections[wp.word].append(tokens[j].word)\n",
    "    return connections"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def split_sentence(sentence):\n",
    "    pattern = re.compile('[。，,.]：')\n",
    "    split = pattern.sub(' ', sentence).split()  # split sentence\n",
    "    pattern.sub()\n",
    "    return split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [],
   "source": [
    "pattern = re.compile('[。，,.?？!！]')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [],
   "source": [
    "string1 = '''2013 3 月份官方突然宣布回归中国，预示着很快就有夏普新机在中国登场了。那么，第一款夏普手机什么时候登陆中国呢？又会是怎么样的手机呢？ 近日，一款型号为 FS8016 的夏普神秘新机悄然出现在 GeekBench 的跑分库上。从其中相关信息了解到，这款机子并非旗舰定位，所搭载的是高通骁龙 660 660 是高通今年最受瞩目的芯片之一，采用 14 纳米工艺，八个 Kryo 260 核心设计，集成 Adreno 512 GPU 和 X12 LTE 660 OPPO 660 OPPO 660 2013 年推出全球首款全面屏手机 EDGEST 302SH 至今，夏普手机推出了多达 28 款的全面屏手机。'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [],
   "source": [
    "pattern = re.compile('[。，,.?？!！]')\n",
    "flags = pattern.findall(string1)\n",
    "sentences = pattern.sub('***', string1)\n",
    "sentences = sentences.split('***')\n",
    "sentences = [sen for sen in sentences if sen != '']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['，', '。', '，', '？', '？', '，', '。', '，', '，', '，', '，', '，', '，', '。']"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "flags"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(14, 14)"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(flags), len(sentences)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['2013 3 月份官方突然宣布回归中国',\n",
       " '预示着很快就有夏普新机在中国登场了',\n",
       " '那么',\n",
       " '第一款夏普手机什么时候登陆中国呢',\n",
       " '又会是怎么样的手机呢',\n",
       " ' 近日',\n",
       " '一款型号为 FS8016 的夏普神秘新机悄然出现在 GeekBench 的跑分库上',\n",
       " '从其中相关信息了解到',\n",
       " '这款机子并非旗舰定位',\n",
       " '所搭载的是高通骁龙 660 660 是高通今年最受瞩目的芯片之一',\n",
       " '采用 14 纳米工艺',\n",
       " '八个 Kryo 260 核心设计',\n",
       " '集成 Adreno 512 GPU 和 X12 LTE 660 OPPO 660 OPPO 660 2013 年推出全球首款全面屏手机 EDGEST 302SH 至今',\n",
       " '夏普手机推出了多达 28 款的全面屏手机']"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sentences"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "sim = [1, 2, 3, 4, 10, 1, 2]\n",
    "window = 1\n",
    "wight = np.array([0.25,0.5,0.25])\n",
    "sim = [sim[0]]*window + sim + [sim[-1]]*window\n",
    "sim = np.array(sim)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [],
   "source": [
    "sim = [np.dot(sim[i-window:i+window+1],wight) for i in range(window, len(sim)-2*window+1)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1.25, 2.0, 3.0, 5.25, 6.25, 3.5, 1.75]"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([13,4])\n",
    "b = np.array([4,5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1,3,4,5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Building prefix dict from the default dictionary ...\n",
      "Loading model from cache C:\\Users\\Oliver\\AppData\\Local\\Temp\\jieba.cache\n",
      "Loading model cost 0.975 seconds.\n",
      "Prefix dict has been built succesfully.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4 从其中相关信息了解到，这款机子并非旗舰定位，所搭载的是高通骁龙 660 660 是高通今年最受瞩目的芯片之一，采用 14 纳米工艺，八个 Kryo 260 核心设计，集成 Adreno 512 GPU 和 X12 LTE 660 OPPO 660 OPPO 660 2013 年推出全球首款全面屏手机 EDGEST 302SH 至今，夏普手机推出了多达 28 款的全面屏手机 0.2766847893600218\n",
      "1 那么，第一款夏普手机什么时候登陆中国呢 0.1951213824940844\n",
      "0 2013 3 月份官方突然宣布回归中国，预示着很快就有夏普新机在中国登场了 0.1921992255134766\n"
     ]
    }
   ],
   "source": [
    "tr4s = TextRank4Sentence()\n",
    "tr4s.analyze(text=string1, lower=True, source = 'all_filters')\n",
    "for item in tr4s.get_key_sentences(num=3):\n",
    "    print(item.index,item.sentence,item.weight)  # index是语句在文本中位置，weight是权重"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [],
   "source": [
    "import operator\n",
    "from functools import reduce"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [[1,3,4],[4,5,6]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = reduce(operator.add, a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 4, 4, 5, 6]"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = [1,3,4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[1, 3, 4], [1, 3, 4], [4, 5, 6]]"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[b] + a "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([1,2,3,4,6,2.5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 5, 2, 3, 4]"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = list(np.argsort(a))\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [],
   "source": [
    "b.reverse()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.sort()\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "string = '''网易娱乐7月21日报道 林肯公园主唱查斯特·贝宁顿Chester Bennington于今天早上，在洛杉矶帕洛斯弗迪斯的一个私人庄园自缢身亡，年仅41岁。此消息已得到洛杉矶警方证实。\n",
    "\n",
    "　　洛杉矶警方透露，Chester的家人正在外地度假，Chester独自在家，上吊地点是家里的二楼。一说是一名音乐公司工作人员来家里找他时发现了尸体，也有人称是佣人最早发现其死亡。\n",
    "\n",
    "　　林肯公园另一位主唱麦克·信田确认了Chester Bennington自杀属实，并对此感到震惊和心痛，称稍后官方会发布声明。Chester昨天还在推特上转发了一条关于曼哈顿垃圾山的新闻。粉丝们纷纷在该推文下留言，不相信Chester已经走了。\n",
    "　　外媒猜测，Chester选择在7月20日自杀的原因跟他极其要好的朋友、Soundgarden(声音花园)乐队以及Audioslave乐队主唱Chris Cornell有关，因为7月20日是Chris Cornell的诞辰。而Chris Cornell于今年5月17日上吊自杀，享年52岁。Chris去世后，Chester还为他写下悼文。\n",
    "　　对于Chester的自杀，亲友表示震惊但不意外，因为Chester曾经透露过想自杀的念头，他曾表示自己童年时被虐待，导致他医生无法走出阴影，也导致他长期酗酒和嗑药来疗伤。目前，洛杉矶警方仍在调查Chester的死因。\n",
    "　　据悉，Chester与毒品和酒精斗争多年，年幼时期曾被成年男子性侵，导致常有轻生念头。Chester生前有过2段婚姻，育有6个孩子。\n",
    "　　林肯公园在今年五月发行了新专辑《多一丝曙光One More Light》，成为他们第五张登顶Billboard排行榜的专辑。而昨晚刚刚发布新单《Talking To Myself》MV。'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'网易娱乐7月21日报道 林肯公园主唱查斯特·贝宁顿Chester Bennington于今天早上，在洛杉矶帕洛斯弗迪斯的一个私人庄园自缢身亡，年仅41岁。此消息已得到洛杉矶警方证实。\\n\\n\\u3000\\u3000洛杉矶警方透露，Chester的家人正在外地度假，Chester独自在家，上吊地点是家里的二楼。一说是一名音乐公司工作人员来家里找他时发现了尸体，也有人称是佣人最早发现其死亡。\\n\\n\\u3000\\u3000林肯公园另一位主唱麦克·信田确认了Chester Bennington自杀属实，并对此感到震惊和心痛，称稍后官方会发布声明。Chester昨天还在推特上转发了一条关于曼哈顿垃圾山的新闻。粉丝们纷纷在该推文下留言，不相信Chester已经走了。\\n\\u3000\\u3000外媒猜测，Chester选择在7月20日自杀的原因跟他极其要好的朋友、Soundgarden(声音花园)乐队以及Audioslave乐队主唱Chris Cornell有关，因为7月20日是Chris Cornell的诞辰。而Chris Cornell于今年5月17日上吊自杀，享年52岁。Chris去世后，Chester还为他写下悼文。\\n\\u3000\\u3000对于Chester的自杀，亲友表示震惊但不意外，因为Chester曾经透露过想自杀的念头，他曾表示自己童年时被虐待，导致他医生无法走出阴影，也导致他长期酗酒和嗑药来疗伤。目前，洛杉矶警方仍在调查Chester的死因。\\n\\u3000\\u3000据悉，Chester与毒品和酒精斗争多年，年幼时期曾被成年男子性侵，导致常有轻生念头。Chester生前有过2段婚姻，育有6个孩子。\\n\\u3000\\u3000林肯公园在今年五月发行了新专辑《多一丝曙光One More Light》，成为他们第五张登顶Billboard排行榜的专辑。而昨晚刚刚发布新单《Talking To Myself》MV。'"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "string.replace('\\\\n','')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(dtype('float64'), dtype('float64'))"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp_test.dtype, sen_mat.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.save('test',temp_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.save('u',u)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.matrix"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(u)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [],
   "source": [
    "del sen_mat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "sent_mat = np.load('test.npy')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[-1.17363378e+00, -1.59759730e+00, -2.27858013e-03, ...,\n",
       "         -2.46709371e-01, -9.13565522e-01, -1.90938040e+00],\n",
       "        [-1.12906535e+00, -1.01443695e+00,  1.56332786e-02, ...,\n",
       "         -1.90675631e+00, -7.49669579e-01,  3.33122243e-01],\n",
       "        [ 9.13327333e-01,  1.39058501e+00, -6.53449206e-03, ...,\n",
       "          1.10634503e+00, -3.67519412e-01, -2.69168192e-01],\n",
       "        ...,\n",
       "        [ 1.40592452e+00,  1.34845206e+00,  2.50960286e-02, ...,\n",
       "          1.35286856e+00,  4.24253547e-01,  3.15313748e+00],\n",
       "        [ 1.44648896e+00,  1.74464158e+00,  1.63353062e-02, ...,\n",
       "          3.96071918e-01,  1.54456767e+00,  3.78070879e+00],\n",
       "        [ 1.93919053e+00,  2.43732186e+00,  1.29721968e-02, ...,\n",
       "          2.25198432e+00,  1.37632574e-02,  4.50754860e+00]])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sent_mat = np.mat(sent_mat)\n",
    "sent_mat = sent_mat* 1e6\n",
    "sent_mat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-6.43929354e-15 -1.11022302e-14 -2.73218947e-17 ... -4.63518113e-15\n",
      "  -5.21804822e-15 -2.59792188e-14]\n",
      " [-4.44089210e-16  1.26565425e-14 -1.04083409e-17 ...  8.88178420e-16\n",
      "  -4.21884749e-15 -1.64313008e-14]\n",
      " [ 2.99760217e-15  3.99680289e-15  3.90312782e-17 ...  1.11022302e-15\n",
      "   1.11022302e-15  7.93809463e-15]\n",
      " ...\n",
      " [ 1.11022302e-15  8.88178420e-16  0.00000000e+00 ...  8.88178420e-16\n",
      "   0.00000000e+00  1.33226763e-15]\n",
      " [ 1.55431223e-15  1.99840144e-15  0.00000000e+00 ...  5.55111512e-16\n",
      "   8.88178420e-16  3.55271368e-15]\n",
      " [ 1.11022302e-15  8.88178420e-16 -1.73472348e-18 ...  1.77635684e-15\n",
      "   7.80625564e-17  0.00000000e+00]]\n"
     ]
    }
   ],
   "source": [
    "#sent_mat = temp_test\n",
    "#sent_mat = np.mat(sent_mat)\n",
    "u, s, vh = np.linalg.svd(sent_mat,full_matrices=1,compute_uv=1)\n",
    "temp = u * u.T * sent_mat\n",
    "sent_mat = sent_mat - temp\n",
    "print(sent_mat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Design Software\\Anaconda\\envs\\py36\\lib\\site-packages\\smart_open\\smart_open_lib.py:398: UserWarning: This function is deprecated, use smart_open.open instead. See the migration notes for details: https://github.com/RaRe-Technologies/smart_open/blob/master/README.rst#migrating-to-the-new-open-function\n",
      "  'See the migration notes for details: %s' % _MIGRATION_NOTES_URL\n"
     ]
    }
   ],
   "source": [
    "fname = Myconfig.get_path('vec.kv')  # 或取模型目录\n",
    "model_wv = KeyedVectors.load(fname, mmap='r')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_wv  = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import gc\n",
    "gc.collect()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "code_folding": [
     119,
     134,
     140,
     153,
     172
    ]
   },
   "outputs": [],
   "source": [
    "from gensim.models import KeyedVectors\n",
    "import numpy as np\n",
    "from textrank4zh import TextRank4Keyword, TextRank4Sentence\n",
    "from pyltp import SentenceSplitter\n",
    "import pickle\n",
    "from myconfig import Myconfig\n",
    "import re\n",
    "import jieba\n",
    "import operator\n",
    "from functools import reduce\n",
    "from gensim.models import LdaModel\n",
    "from gensim.corpora import Dictionary\n",
    "\n",
    "temp_test = None\n",
    "class SentenceEmbedding:\n",
    "    # 句子向量化类\n",
    "    def __init__(self):\n",
    "        self.word_frequence = self.__get_word_frequence()\n",
    "\n",
    "    def get_sentences_vec(self, model_wv, sent_list):\n",
    "        global temp_test\n",
    "        # 句子向量化处理\n",
    "        a = 0.001\n",
    "        row = model_wv.vector_size\n",
    "        col = len(sent_list)\n",
    "        sent_mat = np.zeros((row, col))\n",
    "        for i, sent in enumerate(sent_list):\n",
    "            length = len(sent)\n",
    "            if length == 0: continue\n",
    "            sent_vec = np.zeros(row)\n",
    "            for word in sent:\n",
    "                pw = self.word_frequence[word]\n",
    "                if pw == 0: continue\n",
    "                w = a / (a + pw)\n",
    "                # print(w)\n",
    "                try:\n",
    "                    vec = np.array(model_wv[word])\n",
    "                    sent_vec += w * vec\n",
    "                except:\n",
    "                    pass\n",
    "            sent_mat[:, i] += sent_vec\n",
    "            sent_mat[:, i] /= length\n",
    "\n",
    "        # PCA处理\n",
    "        \n",
    "        sent_mat *= 1e6\n",
    "        sent_mat = np.mat(sent_mat)\n",
    "        u, s, vh = np.linalg.svd(sent_mat)\n",
    "        sent_mat = sent_mat - u * u.T * sent_mat\n",
    "        return sent_mat\n",
    "\n",
    "    def __get_word_frequence(self):\n",
    "        # 这里不做停用次处理，直接在计算句子向量时候，如果找不到该词，直接跳过\n",
    "        path = Myconfig.get_path('frequency.txt')\n",
    "        assert path\n",
    "        with open(path, 'rb') as f:\n",
    "            word_frequence = pickle.load(f)\n",
    "        return word_frequence\n",
    "\n",
    "    # 计算余弦相似度\n",
    "    def cos_similarity(self, v1, v2):\n",
    "        assert isinstance(v1, np.ndarray)\n",
    "        assert isinstance(v2, np.ndarray)\n",
    "        # 输入向量维度不一致\n",
    "        if len(v1) != len(v2):\n",
    "            return 0\n",
    "        if np.linalg.norm(v2) == 0 or np.linalg.norm(v1) == 0:\n",
    "            return 0\n",
    "        return np.vdot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2))\n",
    "\n",
    "    # 返回句子向量矩阵中各列向量与第一列向量的相似度\n",
    "    def __calcu_similarity(self, sent_mat):\n",
    "        assert (isinstance(sent_mat, np.ndarray) or isinstance(sent_mat, np.matrix))\n",
    "        # 采用点积的方法计算\n",
    "        first = np.array(sent_mat[:, 0]).flatten()\n",
    "        col = sent_mat.shape[1]\n",
    "        sims = []\n",
    "        for i in range(1, col):\n",
    "            vec = np.array(sent_mat[:, i]).flatten()\n",
    "            sims.append(self.cos_similarity(first, vec))\n",
    "        return sims\n",
    "\n",
    "    # 获取相似度结果#输入句子中每一句和首句的相似度\n",
    "    def get_similarity_result(self, model_wv, sent_list):\n",
    "        sent_mat = self.get_sentences_vec(model_wv, sent_list)\n",
    "        sim = self.__calcu_similarity(sent_mat)\n",
    "        return sim\n",
    "\n",
    "\n",
    "class Summarization:\n",
    "    def __init__(self):\n",
    "        self.position_re_weight = True\n",
    "        self.Sen_Embedding = SentenceEmbedding()\n",
    "        self.stopwords = self.__get_stopwords()\n",
    "        fname = Myconfig.get_path('vec.kv')  # 或取模型目录\n",
    "        assert fname\n",
    "        self.model_wv = KeyedVectors.load(fname, mmap='r')\n",
    "\n",
    "    def __get_stopwords(self):\n",
    "        path = Myconfig.get_path('stopwords.txt')\n",
    "        stopwords = []\n",
    "        with open(path, encoding='GBK') as f:\n",
    "            line = f.readline()\n",
    "            while line != '':\n",
    "                stopwords.append(line.strip('\\n'))\n",
    "                line = f.readline()\n",
    "        stopwords.append(' ')\n",
    "        return set(stopwords)\n",
    "\n",
    "    def __get_keyword(self, string):\n",
    "        tr4w = TextRank4Keyword()\n",
    "        tr4w.analyze(text=string, lower=True, window=4)\n",
    "        keyword_items = tr4w.get_keywords(10, word_min_len=2)\n",
    "        #把权重标准化\n",
    "        keyword_items = sorted(keyword_items, key= lambda x:x.weight)\n",
    "        over_length = keyword_items[-1].weight\n",
    "        for wp in keyword_items:\n",
    "            wp.weight /= over_length\n",
    "        return keyword_items\n",
    "\n",
    "    # 用正则表达式进行切句\n",
    "    def __split_sentence(self, string):\n",
    "        pattern = re.compile('[。，,.?？!！\"\"“”]')\n",
    "        pattern1 = re.compile('\\w+?([。，,.?？!！\"\"“”])')\n",
    "        flags = pattern1.findall(string)\n",
    "        sentences = pattern.sub('***', string).split('***')\n",
    "        sentences = [sen for sen in sentences if sen != '']\n",
    "        if (len(sentences) > len(flags)): flags.append('.')\n",
    "        # 把句子长度小于4的剔除，一般这些都是转折等过渡语句，会干扰句子提取\n",
    "        filter_index = [i for i in range(len(sentences)) if len(sentences[i]) >= 4]\n",
    "        sentences = [sentences[i] for i in filter_index]\n",
    "        flags = [flags[i] for i in filter_index]\n",
    "\n",
    "        return sentences, flags\n",
    "\n",
    "    # 用pyltp模型进行切句\n",
    "    def __cut_sentence(self, string):\n",
    "        \"\"\"@string contain many sentence\"\"\"\n",
    "        sents = SentenceSplitter.split(string)  # 分句\n",
    "        sents = [sen for sen in sents if len(sen) > 4]\n",
    "        return sents, None\n",
    "\n",
    "    def __get_tokens(self, sentences):\n",
    "        sen_tokens = []\n",
    "        for i, sen in enumerate(sentences):\n",
    "            sen_tokens.append([])\n",
    "            words = jieba.cut(sen)\n",
    "            for wp in words:\n",
    "                if wp not in self.stopwords:\n",
    "                    sen_tokens[i].append(wp)\n",
    "        return sen_tokens\n",
    "\n",
    "    #获取文章主题\n",
    "    #可以根据文章主题和摘要主题进行相似度计算，如果相似度过低，\n",
    "    #可以重新调整各方面权重，重新提取摘要，单句进行主题对比LDA模型效果不好，词太少\n",
    "    def __theme_re_weight(self, tokens):\n",
    "        dictionary = Dictionary(tokens)\n",
    "        corpus = [dictionary.doc2bow(text) for text in tokens]\n",
    "        lda = LdaModel(corpus=corpus, id2word=dictionary, num_topics=2, passes=20)\n",
    "        topic = []\n",
    "        topic.append(lda.show_topic(topicid=0, topn=8))\n",
    "        topic.append(lda.show_topic(topicid=1, topn=8))\n",
    "        return topic\n",
    "\n",
    "    def __knn_soft(self, sim):\n",
    "        window = 2\n",
    "        wight = np.array([0.1, 0.125,0.5, 0.125,0.1])\n",
    "        sim = [sim[0]] * window + sim + [sim[-1]] * window\n",
    "        sim = np.array(sim)\n",
    "        sim = [np.dot(sim[i - window:i + window + 1], wight)\n",
    "               for i in range(window, len(sim) - window)]\n",
    "        return sim\n",
    "\n",
    "    # 考虑标题的影响权重\n",
    "    def __title_re_weight(self, sim, sim_title):\n",
    "        sim = np.array(sim)\n",
    "        sim_title = np.array(sim_title)\n",
    "        p = 0.7\n",
    "        sim = p * sim + (1 - p) * sim_title\n",
    "        return list(sim)\n",
    "\n",
    "    # 考虑关键字对摘要的影响权重\n",
    "    def __keywords_re_weight(self, keywords, sim, tokens):\n",
    "        for wp in keywords:\n",
    "            for i, token in enumerate(tokens):\n",
    "                if wp.word in token:\n",
    "                    sim[i] = sim[i] + 0.02*wp.weight  # 添加关键字的权重\n",
    "        return sim\n",
    "\n",
    "    # 考虑首位句子的影响权重\n",
    "    def __startend_re_weight(self, sents, sim):\n",
    "        if(len(sents[0]) > 20):\n",
    "            sim[0] = sim[0] + 0.1\n",
    "        return sim\n",
    "\n",
    "    def get_summrazation(self, string, num, title=None):\n",
    "        #sentences, flags = self.__split_sentence(string)\n",
    "        sentences, flags = self.__cut_sentence(string)\n",
    "        tokens = self.__get_tokens(sentences)\n",
    "        tokens_all = reduce(operator.add, tokens)\n",
    "        new_tokens = [tokens_all] + tokens\n",
    "        sim = self.Sen_Embedding.get_similarity_result(self.model_wv, new_tokens)\n",
    "        print('11111',len(tokens), len(sim))\n",
    "        #test(sentences,sim)#testpoint\n",
    "        assert len(sim) == len(tokens)\n",
    "        keywords = self.__get_keyword(string)\n",
    "        #print(tokens)\n",
    "        # 根据关键字重新更新一次权值\n",
    "        sim = self.__keywords_re_weight(keywords, sim, tokens)\n",
    "        #test(sentences,sim)#testpoint\n",
    "        \n",
    "        # 如果有标题，则根据标题更新一次权值\n",
    "        if title:\n",
    "            title_tokens = self.__get_tokens([title])\n",
    "            new_tokens = title_tokens + tokens\n",
    "            sim_title = self.Sen_Embedding.get_similarity_result(self.model_wv, new_tokens)\n",
    "            print('22222',len(new_tokens), len(sim_title))\n",
    "            sim = self.__title_re_weight(sim, sim_title)\n",
    "\n",
    "        # 根据首尾位置更新一次权值\n",
    "        if self.position_re_weight:\n",
    "            sim = self.__startend_re_weight(sentences,sim)\n",
    "            #test(sentences,sim)#testpoint\n",
    "        \n",
    "        sim = self.__knn_soft(sim) ##knn soft\n",
    "        #test(sentences,sim)#testpoint\n",
    "        \n",
    "        assert len(sim) == len(tokens)\n",
    "        index = list(np.argsort(sim))\n",
    "        index = index[-num:]  ##取值最高的num项\n",
    "        index.sort()  ##排序\n",
    "\n",
    "        # 把标点也合并\n",
    "        abstract = []\n",
    "        if flags:\n",
    "            for i in index:\n",
    "                abstract.append(sentences[i])\n",
    "                abstract.append(flags[i])\n",
    "        else:\n",
    "            abstract = [sentences[i] for i in index]\n",
    "\n",
    "        topic = self.__theme_re_weight(tokens)\n",
    "\n",
    "        keywords = [(wp.word, wp.weight) for wp in keywords]\n",
    "        # for wp in keywords:\n",
    "        #     result['keywords'].append({'cat': 'a', 'name': wp.word, 'value': 30, 'pro':wp.weight})\n",
    "\n",
    "        return ''.join(abstract), keywords,topic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1.17.2'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "13\n"
     ]
    }
   ],
   "source": [
    "sim = [1,2,3,4,5,6,7,8,9]\n",
    "window = 2\n",
    "wight = np.array([0.1, 0.125,0.5, 0.125,0.1])\n",
    "sim = [sim[0]] * window + sim + [sim[-1]] * window\n",
    "print(len(sim))\n",
    "sim = np.array(sim)\n",
    "sim = [np.dot(sim[i - window:i + window + 1], wight)\n",
    "       for i in range(window, len(sim) - window)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.5"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(wight,[6,7,8,9,9])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1.275, 2.0, 2.85, 3.8000000000000003, 4.75, 5.7, 6.65, 7.5, 8.225]"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "tr4w = TextRank4Keyword()\n",
    "tr4w.analyze(text=string, lower=True, window=4)\n",
    "keyword_items = tr4w.get_keywords(10, word_min_len=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 0 ns\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[{'word': '乐队', 'weight': 0.1911111622895743},\n",
       " {'word': '警方', 'weight': 0.1980252249511147},\n",
       " {'word': 'chris', 'weight': 0.19806924541423443},\n",
       " {'word': 'bennington', 'weight': 0.20362887128640755},\n",
       " {'word': '发布', 'weight': 0.2141050083674248},\n",
       " {'word': '主唱', 'weight': 0.2533254079279914},\n",
       " {'word': '自杀', 'weight': 0.26922750610213825},\n",
       " {'word': '导致', 'weight': 0.2882232067223095},\n",
       " {'word': '洛杉矶', 'weight': 0.3122952749774781},\n",
       " {'word': 'chester', 'weight': 1.0}]"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "keyword_items = sorted(keyword_items, key= lambda x:x.weight)\n",
    "over_length = keyword_items[-1].weight\n",
    "for wp in keyword_items:\n",
    "    wp.weight /= over_length\n",
    "keyword_items"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "def test(sens, sim):\n",
    "    print('##################################')\n",
    "    index = list(np.argsort(sim))\n",
    "    index.reverse()\n",
    "    for i in index:\n",
    "        print(sim[i], sens[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Design Software\\Anaconda\\envs\\py36\\lib\\site-packages\\smart_open\\smart_open_lib.py:398: UserWarning: This function is deprecated, use smart_open.open instead. See the migration notes for details: https://github.com/RaRe-Technologies/smart_open/blob/master/README.rst#migrating-to-the-new-open-function\n",
      "  'See the migration notes for details: %s' % _MIGRATION_NOTES_URL\n"
     ]
    }
   ],
   "source": [
    "My_Summarization = Summarization()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "string ='''据国外媒体报道，据英国《卫报》报道，热带飓风“阿加莎”31日席卷中美洲，它带来的倾盆大雨已经夺去100人的性命。在这场飓风的影响下，危地马拉首都危地马拉城出现一个深达60米的塌陷洞，据说有一栋3层建筑坠入洞中。    受2010年首个太平洋热带风暴影响，危地马拉城积聚了1米多深的雨水，这场风暴还影响到萨尔瓦多和洪都拉斯。据报告上称，目前危地马拉至少已有113人丧生，大约有50人失踪，营救队员正在一片瓦砾中进行搜救。    这个直径30米的塌陷洞位于危地马拉北部地区。当地居民称，雨水和排水系统不完善导致地面塌陷。当地报道表示，在那座3层建筑物坠入地洞时，至少有1人丧生。2007年，这一地区出现类似塌陷洞，当时有3人丧生。    危地马拉是受“阿加莎”影响最严重的一个国家，经证实，该地目前死亡人数已达92人，搜救人员进入偏远农村后，这一数字有可能还会继续上升。有大约10万人被迫撤离家园。警方称，萨尔瓦多有9人死亡，洪都拉斯有12人丧生。    阿马蒂特兰的卡尔洛塔·拉莫斯站在几乎被淤泥淹没的房屋前悲伤地说：“没有人可以帮助我。我眼睁睁看着雨水冲走了一切。'''\n",
    "title = '''危地马拉受热带风暴影响出现60米深巨大陷坑'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'据国外媒体报道，据英国《卫报》报道，热带飓风“阿加莎”31日席卷中美洲，它带来的倾盆大雨已经夺去100人的性命。在这场飓风的影响下，危地马拉首都危地马拉城出现一个深达60米的塌陷洞，据说有一栋3层建筑坠入洞中。    受2010年首个太平洋热带风暴影响，危地马拉城积聚了1米多深的雨水，这场风暴还影响到萨尔瓦多和洪都拉斯。据报告上称，目前危地马拉至少已有113人丧生，大约有50人失踪，营救队员正在一片瓦砾中进行搜救。    这个直径30米的塌陷洞位于危地马拉北部地区。当地居民称，雨水和排水系统不完善导致地面塌陷。当地报道表示，在那座3层建筑物坠入地洞时，至少有1人丧生。2007年，这一地区出现类似塌陷洞，当时有3人丧生。    危地马拉是受“阿加莎”影响最严重的一个国家，经证实，该地目前死亡人数已达92人，搜救人员进入偏远农村后，这一数字有可能还会继续上升。有大约10万人被迫撤离家园。警方称，萨尔瓦多有9人死亡，洪都拉斯有12人丧生。    阿马蒂特兰的卡尔洛塔·拉莫斯站在几乎被淤泥淹没的房屋前悲伤地说：“没有人可以帮助我。我眼睁睁看着雨水冲走了一切。'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "string = string.replace('\\u3000','').replace('\\n','')\n",
    "string"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11111 13 13\n",
      "Wall time: 415 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "result = My_Summarization.get_summrazation(string,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'据国外媒体报道，据英国《卫报》报道，热带飓风“阿加莎”31日席卷中美洲，它带来的倾盆大雨已经夺去100人的性命。在这场飓风的影响下，危地马拉首都危地马拉城出现一个深达60米的塌陷洞，据说有一栋3层建筑坠入洞中。据报告上称，目前危地马拉至少已有113人丧生，大约有50人失踪，营救队员正在一片瓦砾中进行搜救。'"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'据国外媒体报道，据英国《卫报》报道，热带飓风“阿加莎”31日席卷中美洲，它带来的倾盆大雨已经夺去100人的性命。在这场飓风的影响下，危地马拉首都危地马拉城出现一个深达60米的塌陷洞，据说有一栋3层建筑坠入洞中。受2010年首个太平洋热带风暴影响，危地马拉城积聚了1米多深的雨水，这场风暴还影响到萨尔瓦多和洪都拉斯。'"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## LDA主题分类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Design Software\\Anaconda\\envs\\py36\\lib\\site-packages\\gensim\\utils.py:1197: UserWarning: detected Windows; aliasing chunkize to chunkize_serial\n",
      "  warnings.warn(\"detected Windows; aliasing chunkize to chunkize_serial\")\n"
     ]
    }
   ],
   "source": [
    "import codecs\n",
    "\n",
    "from gensim import corpora\n",
    "from gensim.models import LdaModel, LdaMulticore\n",
    "from gensim.corpora import Dictionary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, '0.056*\"Sugar\" + 0.056*\"My\" + 0.056*\"sister\"')\n",
      "(1, '0.062*\"driving\" + 0.035*\"time\" + 0.035*\"practice.\"')\n",
      "(2, '0.079*\"my\" + 0.045*\"sister\" + 0.045*\"father\"')\n",
      "[(0, 0.12893324), (1, 0.65889436), (2, 0.21217237)]\n",
      "Wall time: 34 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "# stopwords = codecs.open('stopWords/1893（utf8）.txt','r',encoding='utf8').readlines()\n",
    "# stopwords = [ w.strip() for w in stopwords ]\n",
    "doc1 = \"Sugar is bad to consume. My sister likes to have sugar, but not my father.\"\n",
    "doc2 = \"My father spends a lot of time driving my sister around to dance practice.\"\n",
    "doc3 = \"Doctors suggest that driving may cause increased stress and blood pressure.\"\n",
    "doc4 = \"Sometimes I feel pressure to perform well at school, but my father never seems to drive my sister to do better.\"\n",
    "doc5 = \"Health experts say that Sugar is not good for your lifestyle.\"\n",
    "\n",
    "# 整合文档数据\n",
    "stop_words = ('is','to','.',',','a','of','and','but','for','that','at','cause','not','around','lot')\n",
    "doc_complete = [doc1, doc2, doc3, doc4, doc5]\n",
    "def clean(doc):\n",
    "    \n",
    "    punc_free = [ch for ch in doc.split() if ch not in stop_words]\n",
    "    return punc_free\n",
    "\n",
    "doc_clean = [clean(doc) for doc in doc_complete]\n",
    "\n",
    "dictionary = corpora.Dictionary(doc_clean)\n",
    "corpus = [dictionary.doc2bow(text) for text in doc_clean]\n",
    "\n",
    "#tfidf = TfidfModel(corpus)\n",
    "#corpus_tfidf = tfidf[corpus]\n",
    "#lda = LdaModel(corpus=corpus_tfidf, id2word=dictionary, num_topics=3,passes=50)\n",
    "lda = LdaModel(corpus=corpus, id2word=dictionary, num_topics=3,passes=2)\n",
    "\n",
    "for topic in lda.print_topics(num_words = 3):\n",
    "    termNumber = topic[0]\n",
    "    print(topic)\n",
    "    \n",
    "lda.show_topic(topicid=0, topn=20)\n",
    "\n",
    "other_texts = [['sugar', 'Health', 'sister'],['sugar', 'Health', 'my']]\n",
    "other_corpus = [dictionary.doc2bow(text) for text in other_texts]\n",
    "unseen_doc = other_corpus[1]\n",
    "vector = lda[unseen_doc]  # get topic probability distribution for a document\n",
    "print(vector)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [],
   "source": [
    "aa = b'{\"text\":\"\",\"num\":\"3\"}'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [],
   "source": [
    "tt = aa.decode('utf-8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(tt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(0, 0.12891178), (1, 0.65852326), (2, 0.21256495)]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lda.get_document_topics(bow=unseen_doc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 0 ns\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "topic_new = []\n",
    "for tp in topic:\n",
    "    temp = []\n",
    "    for wp in tp:\n",
    "        temp.append({\"name\":wp[0] ,'value':wp[1]})\n",
    "    topic_new.append(temp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('My', 0.056150153)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "topic[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "topic = []\n",
    "topic.append(lda.show_topic(topicid=0, topn=20))\n",
    "topic.append(lda.show_topic(topicid=1, topn=20))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 173,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.07800436019897461\n",
      "(0, '0.057*\"Sugar\" + 0.056*\"My\" + 0.056*\"sister\" + 0.056*\"my\"')\n",
      "(1, '0.062*\"driving\" + 0.061*\"suggest\" + 0.061*\"may\" + 0.061*\"blood\"')\n",
      "(2, '0.073*\"my\" + 0.051*\"sister\" + 0.051*\"father\" + 0.029*\"dance\"')\n",
      "[(0, 0.1218094), (1, 0.11241036), (2, 0.7657802)]\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "t1 = time.time()\n",
    "lda = LdaModel(corpus=corpus, id2word=dictionary, num_topics=3,passes=10)\n",
    "print(time.time()-t1)\n",
    "for topic in lda.print_topics(num_words = 4):\n",
    "    termNumber = topic[0]\n",
    "    print(topic)\n",
    "\n",
    "other_texts = [['sugar', 'Health', 'sister'],['sugar', 'Health', 'my']]\n",
    "other_corpus = [dictionary.doc2bow(text) for text in other_texts]\n",
    "unseen_doc = other_corpus[0]\n",
    "vector = lda[unseen_doc]  # get topic probability distribution for a document\n",
    "print(vector)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(0, 1), (3, 1), (6, 1)]"
      ]
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> texts = [['human', 'interface', 'computer']]\n",
    ">>> dct = Dictionary(texts)  # initialize a Dictionary\n",
    ">>> dct.add_documents([[\"cat\", \"say\", \"meow\"], [\"dog\",'cat']])  # add more document (extend the vocabulary)\n",
    ">>> dct.doc2bow([\"dog\", \"computer\", \"cat\"])#稀疏向量表示方法\n",
    "#[(0, 1), (1, 1)]，表明id为0,1的词汇出现了1次，这里的0,1是按照dct.token2id的顺序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "string1 = '''新华社北京9月27日电（记者温馨）外交部发言人耿爽27日在例行记\\\n",
    "\t\t者会上表示，中方对日本首相安倍晋三祝贺中华人民共和国成立70周年，并重申对发展中日关\\\n",
    "\t\t系的积极意愿表示赞赏26日，中国驻日使馆举行庆祝新中国成立70周年招待会。日本首相安倍\\\n",
    "\t\t晋三向招待会发送视频祝贺中华人民共和国成立70周年。耿爽说，今年是中华人民共和国成立\\\n",
    "\t\t70周年，日本首相安倍晋三特地发视频向中国人民表达节日祝贺，并重申对发展中日关系的积\\\n",
    "\t\t极意愿，中方对此表示赞赏。中方认为，这体现了当前中日关系改善向好的积极势头。'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_stopwords():\n",
    "    path = Myconfig.get_path('stopwords.txt')\n",
    "    stopwords = []\n",
    "    with open(path) as f:\n",
    "        line = f.readline()\n",
    "        while line != '':\n",
    "            stopwords.append(line.strip('\\n'))\n",
    "            line = f.readline()\n",
    "    stopwords += [' ','\\u3000','\\t','\\n']\n",
    "    return set(stopwords)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, '0.041*\"成立\" + 0.041*\"70\" + 0.041*\"周年\" + 0.032*\"祝贺\" + 0.032*\"表示\" + 0.032*\"中方\"')\n",
      "(1, '0.018*\"周年\" + 0.018*\"70\" + 0.018*\"祝贺\" + 0.018*\"成立\" + 0.018*\"中方\" + 0.018*\"中华人民共和国\"')\n",
      "(2, '0.018*\"成立\" + 0.018*\"表示\" + 0.018*\"日本首相\" + 0.018*\"中华人民共和国\" + 0.018*\"中方\" + 0.018*\"周年\"')\n",
      "[(0, 0.9997276)]\n"
     ]
    }
   ],
   "source": [
    "from myconfig import Myconfig\n",
    "from gensim import corpora\n",
    "from gensim.models import LdaModel, TfidfModel\n",
    "from gensim.corpora import Dictionary\n",
    "import jieba\n",
    "\n",
    "\n",
    "# 整合文档数据\n",
    "stop_words = get_stopwords()\n",
    "\n",
    "def clean(doc):\n",
    "    doc = doc.strip()\n",
    "    tokens = jieba.cut(doc)\n",
    "    doc_cleaned = []\n",
    "    for token in tokens:\n",
    "        if token not in stop_words:\n",
    "            doc_cleaned.append(token)\n",
    "    return [doc_cleaned]\n",
    "\n",
    "doc_clean = clean(string1)\n",
    "\n",
    "dictionary = corpora.Dictionary(doc_clean)\n",
    "corpus = [dictionary.doc2bow(text) for text in doc_clean]\n",
    "\n",
    "#tfidf = TfidfModel(corpus)\n",
    "#corpus_tfidf = tfidf[corpus]\n",
    "#lda = LdaModel(corpus=corpus_tfidf, id2word=dictionary, num_topics=5,passes=50,alpha='auto')\n",
    "lda = LdaModel(corpus=corpus, id2word=dictionary, num_topics=3,passes=50,alpha='auto')\n",
    "\n",
    "for topic in lda.print_topics(num_words = 6):\n",
    "    termNumber = topic[0]\n",
    "    print(topic)\n",
    "    \n",
    "other_texts = [['周年', '成立', '中国'],['sugar', 'Health', 'my']]\n",
    "other_corpus = [dictionary.doc2bow(text) for text in other_texts]\n",
    "unseen_doc = other_corpus[0]\n",
    "vector = lda[unseen_doc]  # get topic probability distribution for a document\n",
    "print(vector)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 215,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.14"
      ]
     },
     "execution_count": 215,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(3.1415,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "string = ''',,上理工与上海交大医学院此前已各自在医工交叉领域开启了前沿探索，但是随着各自研究的深入，双方发现，只有“跨界”携手、深化合作，才能将医工交叉领域推至发展的快车道。\n",
    "　　据中国科学院院士、上海交通大学医学院院长陈国强介绍，未来将主要由上海交大医学院众多医学专家提出改进诊疗手段和医疗服务的关键问题，由上理工优秀的工程科技团队集体攻关。目前，双方以需求为导向启动了40个医工交叉合作项目。\n",
    "　　以升级国产缝合针为例，该项目由上海交通大学医学院附属第九人民医院医生提出原创想法：国外进口的手术缝合针虽使用方便、安全系数高，但高昂的价格令人望而却步；国产缝合针价格便宜但容易弯折并扯破皮肤。随后，针对这一问题，上理工材料学院教师牵头攻关，仅用两个月就拿出了国产替代方案，且制作成本仅为国外的二分之一。\n",
    "　　上理工与上海交大医学院还将通过这些项目培养复合型高层次人才。目前已有200余名上理工研究生进入开展合作的40个项目，并从参与项目的医生中遴选出15名医工交叉博导和38名联合培养导师。据上海理工大学校长丁晓东介绍，每个合作项目配备四至六名研究生，并且每个项目必须有一至两名研究生新生参与，让研究生从入学起就在项目中磨炼'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "string = string.strip().replace(' ','').replace('\\u3000','').replace('\\n','')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "re??"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "88.3 µs ± 1.41 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "pattern = re.compile('[。，,.?？!！\"\"“”]')\n",
    "pattern1 = re.compile('\\w+?([。，,.?？!！\"\"“”])')\n",
    "flags = pattern1.findall(string)\n",
    "sentences = pattern.sub('***', string).split('***')\n",
    "sentences = [sen for sen in sentences if sen != '']\n",
    "if(len(sentences)>len(flags)):flags.append('.')\n",
    "# 把句子长度小于4的剔除，一般这些都是转折等过渡语句，会干扰句子提取\n",
    "filter_index = [i for i in range(len(sentences)) if len(sentences[i]) >= 4]\n",
    "sentences = [sentences[i] for i in filter_index]\n",
    "flags = [flags[i] for i in filter_index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "上理工与上海交大医学院此前已各自在医工交叉领域开启了前沿探索，但是随着各自研究的深入，双方发现，携手、深化合作，才能将医工交叉领域推至发展的快车道。据中国科学院院士、上海交通大学医学院院长陈国强介绍，未来将主要由上海交大医学院众多医学专家提出改进诊疗手段和医疗服务的关键问题，由上理工优秀的工程科技团队集体攻关。双方以需求为导向启动了40个医工交叉合作项目。以升级国产缝合针为例，该项目由上海交通大学医学院附属第九人民医院医生提出原创想法：国外进口的手术缝合针虽使用方便、安全系数高，但高昂的价格令人望而却步；国产缝合针价格便宜但容易弯折并扯破皮肤。针对这一问题，上理工材料学院教师牵头攻关，仅用两个月就拿出了国产替代方案，且制作成本仅为国外的二分之一。上理工与上海交大医学院还将通过这些项目培养复合型高层次人才。目前已有200余名上理工研究生进入开展合作的40个项目，并从参与项目的医生中遴选出15名医工交叉博导和38名联合培养导师。据上海理工大学校长丁晓东介绍，每个合作项目配备四至六名研究生，并且每个项目必须有一至两名研究生新生参与，让研究生从入学起就在项目中磨炼.\n"
     ]
    }
   ],
   "source": [
    "temp = ''\n",
    "for i in range(len(flags)):\n",
    "    temp += sentences[i] + flags[i]\n",
    "\n",
    "print(temp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ",,上理工与上海交大医学院此前已各自在医工交叉领域开启了前沿探索，但是随着各自研究的深入，双方发现，只有“跨界”携手、深化合作，才能将医工交叉领域推至发展的快车道。\n",
      "据中国科学院院士、上海交通大学医学院院长陈国强介绍，未来将主要由上海交大医学院众多医学专家提出改进诊疗手段和医疗服务的关键问题，由上理工优秀的工程科技团队集体攻关。\n",
      "目前，双方以需求为导向启动了40个医工交叉合作项目。\n",
      "以升级国产缝合针为例，该项目由上海交通大学医学院附属第九人民医院医生提出原创想法：国外进口的手术缝合针虽使用方便、安全系数高，但高昂的价格令人望而却步；\n",
      "国产缝合针价格便宜但容易弯折并扯破皮肤。\n",
      "随后，针对这一问题，上理工材料学院教师牵头攻关，仅用两个月就拿出了国产替代方案，且制作成本仅为国外的二分之一。\n",
      "上理工与上海交大医学院还将通过这些项目培养复合型高层次人才。\n",
      "目前已有200余名上理工研究生进入开展合作的40个项目，并从参与项目的医生中遴选出15名医工交叉博导和38名联合培养导师。\n",
      "据上海理工大学校长丁晓东介绍，每个合作项目配备四至六名研究生，并且每个项目必须有一至两名研究生新生参与，让研究生从入学起就在项目中磨炼\n"
     ]
    }
   ],
   "source": [
    "sents = SentenceSplitter.split(string)# 分句\n",
    "for sen in sents:\n",
    "    print(sen)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['上理工与上海交大医学院此前已各自在医工交叉领域开启了前沿探索', '但是随着各自研究的深入', '双方发现', '携手、深化合作', '才能将医工交叉领域推至发展的快车道', '据中国科学院院士、上海交通大学医学院院长陈国强介绍', '未来将主要由上海交大医学院众多医学专家提出改进诊疗手段和医疗服务的关键问题', '由上理工优秀的工程科技团队集体攻关', '双方以需求为导向启动了40个医工交叉合作项目', '以升级国产缝合针为例', '该项目由上海交通大学医学院附属第九人民医院医生提出原创想法：国外进口的手术缝合针虽使用方便、安全系数高', '但高昂的价格令人望而却步；国产缝合针价格便宜但容易弯折并扯破皮肤', '针对这一问题', '上理工材料学院教师牵头攻关', '仅用两个月就拿出了国产替代方案', '且制作成本仅为国外的二分之一', '上理工与上海交大医学院还将通过这些项目培养复合型高层次人才', '目前已有200余名上理工研究生进入开展合作的40个项目', '并从参与项目的医生中遴选出15名医工交叉博导和38名联合培养导师', '据上海理工大学校长丁晓东介绍', '每个合作项目配备四至六名研究生', '并且每个项目必须有一至两名研究生新生参与', '让研究生从入学起就在项目中磨炼']\n",
      "[',,上理工与上海交大医学院此前已各自在医工交叉领域开启了前沿探索，但是随着各自研究的深入，双方发现，只有“跨界”携手、深化合作，才能将医工交叉领域推至发展的快车道。', '据中国科学院院士、上海交通大学医学院院长陈国强介绍，未来将主要由上海交大医学院众多医学专家提出改进诊疗手段和医疗服务的关键问题，由上理工优秀的工程科技团队集体攻关。', '目前，双方以需求为导向启动了40个医工交叉合作项目。', '以升级国产缝合针为例，该项目由上海交通大学医学院附属第九人民医院医生提出原创想法：国外进口的手术缝合针虽使用方便、安全系数高，但高昂的价格令人望而却步；', '国产缝合针价格便宜但容易弯折并扯破皮肤。', '随后，针对这一问题，上理工材料学院教师牵头攻关，仅用两个月就拿出了国产替代方案，且制作成本仅为国外的二分之一。', '上理工与上海交大医学院还将通过这些项目培养复合型高层次人才。', '目前已有200余名上理工研究生进入开展合作的40个项目，并从参与项目的医生中遴选出15名医工交叉博导和38名联合培养导师。', '据上海理工大学校长丁晓东介绍，每个合作项目配备四至六名研究生，并且每个项目必须有一至两名研究生新生参与，让研究生从入学起就在项目中磨炼']\n"
     ]
    }
   ],
   "source": [
    "print(sentences)\n",
    "print(sents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "from flask import jsonify"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = {'keywords': [{'cat': 0, 'name': '双方', 'value': 40.45, 'pro': 0.01637311936196191}, \n",
    "                     {'cat': 1, 'name': '国产', 'value': 40.55, 'pro': 0.016426144264215112}]}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "ename": "RuntimeError",
     "evalue": "Working outside of application context.\n\nThis typically means that you attempted to use functionality that needed\nto interface with the current application object in some way. To solve\nthis, set up an application context with app.app_context().  See the\ndocumentation for more information.",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-44-04776b837acc>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mjsonify\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mD:\\Design Software\\Anaconda\\envs\\py36\\lib\\site-packages\\flask\\json\\__init__.py\u001b[0m in \u001b[0;36mjsonify\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m    356\u001b[0m     \u001b[0mseparators\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m\",\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\":\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    357\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 358\u001b[1;33m     \u001b[1;32mif\u001b[0m \u001b[0mcurrent_app\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mconfig\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"JSONIFY_PRETTYPRINT_REGULAR\"\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mcurrent_app\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdebug\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    359\u001b[0m         \u001b[0mindent\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    360\u001b[0m         \u001b[0mseparators\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m\", \"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\": \"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Design Software\\Anaconda\\envs\\py36\\lib\\site-packages\\werkzeug\\local.py\u001b[0m in \u001b[0;36m__getattr__\u001b[1;34m(self, name)\u001b[0m\n\u001b[0;32m    346\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mname\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m\"__members__\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    347\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mdir\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_get_current_object\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 348\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mgetattr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_get_current_object\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    349\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    350\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m__setitem__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Design Software\\Anaconda\\envs\\py36\\lib\\site-packages\\werkzeug\\local.py\u001b[0m in \u001b[0;36m_get_current_object\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    305\u001b[0m         \"\"\"\n\u001b[0;32m    306\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mhasattr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__local\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"__release_local__\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 307\u001b[1;33m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__local\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    308\u001b[0m         \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    309\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mgetattr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__local\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__name__\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Design Software\\Anaconda\\envs\\py36\\lib\\site-packages\\flask\\globals.py\u001b[0m in \u001b[0;36m_find_app\u001b[1;34m()\u001b[0m\n\u001b[0;32m     50\u001b[0m     \u001b[0mtop\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_app_ctx_stack\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtop\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     51\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mtop\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 52\u001b[1;33m         \u001b[1;32mraise\u001b[0m \u001b[0mRuntimeError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m_app_ctx_err_msg\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     53\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mtop\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mapp\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     54\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mRuntimeError\u001b[0m: Working outside of application context.\n\nThis typically means that you attempted to use functionality that needed\nto interface with the current application object in some way. To solve\nthis, set up an application context with app.app_context().  See the\ndocumentation for more information."
     ]
    }
   ],
   "source": [
    "jsonify(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
